//
//  FZSendBlogToolBar.swift
//  MyWeiBo
//
//  Created by fangzhengwei on 2017/4/5.
//  Copyright © 2017年 fangzhengwei. All rights reserved.
//

import UIKit

enum composeButtonType : Int {
    case camera
    case mention
    case trend
    case emoticon
    case add
    
}

@available(iOS 9.0, *)
class FZSendBlogToolBar: UIStackView {

    var buttonClosure : ((composeButtonType)->())?
    
    fileprivate var emotionButton : UIButton?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    fileprivate func setupUI(){
        self.distribution = .fillEqually
        self.axis = .horizontal
        
      loadBtns(imageName: "compose_camerabutton_background" , buttonType: .camera)
      loadBtns(imageName: "compose_mentionbutton_background" , buttonType: .mention)
      loadBtns(imageName: "compose_trendbutton_background" , buttonType: .trend)
      emotionButton = loadBtns(imageName: "compose_emoticonbutton_background" , buttonType: .emoticon)
      loadBtns(imageName: "compose_add_background" , buttonType: .add)
        
    }
    
   @discardableResult //忽略返回值
   fileprivate func loadBtns(imageName : String , buttonType : composeButtonType) -> UIButton?{
        let btn = UIButton()
        btn.tag = buttonType.rawValue
        btn.setImage(UIImage(named : imageName), for: .normal)
        btn.setImage(UIImage(named : "\(imageName)_highlighted"), for: .highlighted)
        btn.setBackgroundImage(UIImage(named : "compose_toolbar_background"), for: .normal)
        btn.addTarget(self, action: #selector(clickComposeButton(btn:)), for: .touchUpInside)
        btn.adjustsImageWhenHighlighted = false
        addArrangedSubview(btn)
        return btn
    }
    
    @objc fileprivate func clickComposeButton(btn : UIButton) {
        
        let buttonType = composeButtonType(rawValue: btn.tag)
        if let type = buttonType {
             buttonClosure?(type)
        }
    }
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension FZSendBlogToolBar {
    
    func changeKeyBoardPattern(isEmotion : Bool) {
        
        var imageName : String = ""

        if isEmotion {
            
            imageName = "compose_keyboardbutton_background"
        }else{
            imageName = "compose_emoticonbutton_background"
        }
        
        emotionButton?.setImage(UIImage(named : imageName), for: .normal)
        emotionButton?.setImage(UIImage(named : "\(imageName)_highlighted"), for: .highlighted)
    }
    
    
}













